﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using Viewer.BusinessObject.BusinessEntities;
namespace Viewer.NhomNguoiDung.UserControls
{
    public partial class UCNhomNguoiDung : System.Web.UI.UserControl
    {
        private enum DialogType
        {
            AddNew,
            Edit,
            Detail
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        CommonCode common = new CommonCode();
        public void ShowDialogAddNew()
        {
            __DialogType.Text = DialogType.AddNew.ToString();
            wdUCNhomNguoiDung.Icon = Icon.Add;
            wdUCNhomNguoiDung.Title = "Add New";
            Ext.Net.X.AddScript(grdNguoiDung_NhomNguoiDung.ClientID + ".clear();");            
            wdNguoiDung.BindData();
            //storeLoaiNhom.DataSource = common.LoadDataCommonByCodeKey(common.LoaiNhomNguoiDung);
            //storeLoaiNhom.DataBind();
            btnSave.Show();
            btnShowDialogAddNguoiDung.Disabled = false;            
            txtTenNhom.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            btnSave.Show();
            
            wdUCNhomNguoiDung.Show();
        }

        public void ShowDialogEdit(int pNhomNguoiDungID)
        {
            //storeLoaiNhom.DataSource = common.LoadDataCommonByCodeKey(common.LoaiNhomNguoiDung);
            //storeLoaiNhom.DataBind();
            __DialogType.Text = DialogType.Edit.ToString();
            __NhomNguoiDungID.Text = pNhomNguoiDungID.ToString();
            var oNhomNguoiDung = (new QUNhomNguoiDung()).GetByID(pNhomNguoiDungID);
            storeNguoiDung_NhomNguoiDung.DataSource = oNhomNguoiDung.GetNguoiDung();
            storeNguoiDung_NhomNguoiDung.DataBind();
            txtTenNhom.Text = oNhomNguoiDung.TenNhomNguoiDung;
            txtGhiChu.Text = oNhomNguoiDung.GhiChu;
            //cboLoaiNhom.Value = oNhomNguoiDung.LoaiNhomNguoiDungID;
            wdNguoiDung.BindData();
            btnShowDialogAddNguoiDung.Disabled = false;  
            btnSave.Show();
            wdUCNhomNguoiDung.Show();
        }

        public void ShowDialogDetail(int pNhomNguoiDungID)
        {
            //storeLoaiNhom.DataSource = common.LoadDataCommonByCodeKey(common.LoaiNhomNguoiDung);
            //storeLoaiNhom.DataBind();
            __DialogType.Text = DialogType.Detail.ToString();
            __NhomNguoiDungID.Text = pNhomNguoiDungID.ToString();

            var oNhomNguoiDung = (new QUNhomNguoiDung()).GetByID(pNhomNguoiDungID);
            storeNguoiDung_NhomNguoiDung.DataSource = oNhomNguoiDung.GetNguoiDung();
            storeNguoiDung_NhomNguoiDung.DataBind();
            txtTenNhom.Text = oNhomNguoiDung.TenNhomNguoiDung;
            txtGhiChu.Text = oNhomNguoiDung.GhiChu;
            //cboLoaiNhom.Value = oNhomNguoiDung.LoaiNhomNguoiDungID;
            btnShowDialogAddNguoiDung.Disabled = true;  
            btnSave.Hide();
            wdUCNhomNguoiDung.Show();
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                if (__DialogType.Text == DialogType.AddNew.ToString())
                {
                    SaveNewData();
                    ((NhomNguoiDung)this.Page).LoadData();
                }
                else if (__DialogType.Text == DialogType.Edit.ToString())
                {
                    SaveEditData();
                    ((NhomNguoiDung)this.Page).LoadData();
                }
                wdUCNhomNguoiDung.Hide();
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        private void SaveNewData()
        {
            string[] separator = new string[] { "," };
            var NguoiDungIds = this.__NguoiDungIds.Text.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            var oNhomNguoiDung = new QUNhomNguoiDung();
            oNhomNguoiDung.TenNhomNguoiDung = txtTenNhom.Text.Trim();
            //oNhomNguoiDung.LoaiNhomNguoiDungID = int.Parse(cboLoaiNhom.Value.ToString());
            oNhomNguoiDung.GhiChu = txtGhiChu.Text.Trim();

            foreach (string  nguoidungID in NguoiDungIds)
            {
                var oNhomNguoiDung_Nguoidung = new QUNhomNguoiDung_NguoiDung();
                oNhomNguoiDung_Nguoidung.NguoiDungID = int.Parse(nguoidungID);
                oNhomNguoiDung.QUNhomNguoiDung_NguoiDungs.Add(oNhomNguoiDung_Nguoidung);
            }
            oNhomNguoiDung.Save();
        }

        private void SaveEditData()
        {
            string[] separator = new string[] { "," };
            var NguoiDungIds = this.__NguoiDungIds.Text.Split(separator, StringSplitOptions.RemoveEmptyEntries);
            var oNhomNguoiDung = (new QUNhomNguoiDung()).GetByID(int.Parse(__NhomNguoiDungID.Text));
            oNhomNguoiDung.TenNhomNguoiDung = txtTenNhom.Text.Trim();
            //oNhomNguoiDung.LoaiNhomNguoiDungID = int.Parse(cboLoaiNhom.Value.ToString());
            oNhomNguoiDung.GhiChu = txtGhiChu.Text.Trim();
            oNhomNguoiDung.QUNhomNguoiDung_NguoiDungs.Clear();
            foreach (string nguoidungID in NguoiDungIds)
            {
                var oNhomNguoiDung_Nguoidung = new QUNhomNguoiDung_NguoiDung();
                oNhomNguoiDung_Nguoidung.NguoiDungID = int.Parse(nguoidungID);
                oNhomNguoiDung.QUNhomNguoiDung_NguoiDungs.Add(oNhomNguoiDung_Nguoidung);
            }
            oNhomNguoiDung.Save();
        }
    }
}